home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 …ember: Reference Library / Dev.CD Dec 98 RL1.toast / What's New / Development Kits / AppleShare IP 6.1 SDK / ASIP User Authentication Module / Client UAM SDK / Sample Code / Kleartxt Client UAM / Kleartxt UAM.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-26  |  4.5 KB  |  208 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Kleartxt UAM.h
  3.  
  4.     Contains:    Simple Client UAM
  5.  
  6.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  7. */
  8.  
  9.  
  10.  
  11. #include <String.h>
  12. #include <Resources.h>
  13. #include <A4Stuff.h>
  14.  
  15. #include "Kleartxt UAM.h"
  16. #include "AFPPackets.h"
  17.  
  18. unsigned char    commandBuffer[200];
  19. unsigned char    replyBuffer[512];
  20. StringPtr        gAFPVersion;
  21.  
  22.  
  23.  
  24. StringPtr    FigureAFPVersion(AFPSrvrInfo *,ClientUAMCallbackRec *theCallbacks);
  25.  
  26. //pascal    OSStatus    UAMCall(UAMArgs *theArgs);
  27. pascal    OSStatus    main(UAMArgs *theArgs)
  28. {
  29.     EnterCodeResource();
  30.     OSStatus    error;
  31.     switch(theArgs->command)
  32.     {
  33.         case    kUAMOpen:
  34.             error =    SampleOpen(theArgs);
  35.             break;
  36.                                 
  37.         case    kUAMPWDlog:
  38.             error =    kNotForUs;
  39.             break;
  40.  
  41.         case    kUAMLogin:
  42.             error =    SampleLogin(theArgs);
  43.             break;
  44.         
  45.         case    kUAMVSDlog:
  46.             error =    kNotForUs;
  47.             break;
  48.  
  49.         case    kUAMChgPassDlg:
  50.             error =    kNotForUs;
  51.             break;
  52.  
  53.         case    kUAMChgPass:
  54.             error =    kNotForUs;
  55.             break;
  56.  
  57.         case    kUAMGetInfoSize:
  58.             error =    kNotForUs;
  59.             break;
  60.  
  61.         case    kUAMGetInfo:
  62.             error =    kNotForUs;
  63.             break;
  64.  
  65.         case    kUAMClose:
  66.             error =    noErr;
  67.             break;
  68.  
  69.         default:
  70.             error =    kNotForUs;
  71.             break;
  72.     }
  73.     ExitCodeResource();
  74.     return error;
  75. }
  76.  
  77.  
  78. OSStatus    SampleOpen(UAMArgs *theArgs)
  79.                         
  80. {
  81.     //DebugStr("\pin SampleOpen");
  82.     gAFPVersion = FigureAFPVersion(theArgs->Opt.open.srvrInfo,theArgs->callbacks);
  83.     theArgs->result = kSampleCfg;
  84.     
  85.     return noErr;
  86. }
  87.  
  88.  
  89. OSStatus    SampleLogin(UAMArgs *theArgs){
  90.     OSStatus        theError = kUAMError;
  91.     Ptr                cmd;
  92.     unsigned long    cmdSize;
  93.     Handle            theUAMName;
  94.     UAMMessage        message;
  95.     StringPtr        user = theArgs->Opt.auth.userName;
  96.     unsigned char *    password = theArgs->Opt.auth.password;
  97.     
  98.     //DebugStr("\pin SampleLogin");
  99.  
  100.     if(!gAFPVersion){
  101.         // put up an alert & return userCancelled error
  102.         DebugStr("\pno AFP version");
  103.         return userCanceledErr;
  104.     }
  105.     if(theArgs->callbacks)
  106.     {
  107.         commandBuffer[0] = kFPLogin;
  108.         cmd = (Ptr) &commandBuffer[1];
  109.         memcpy(cmd,(const char *)&gAFPVersion[0],gAFPVersion[0]+1);
  110.         cmd += gAFPVersion[0] + 1;
  111.         
  112.         // get the UAMSTring from the resource
  113.         theUAMName = Get1Resource(kUAMStr,kUAMProtoName);
  114.         if(!theUAMName)
  115.             return ResError();    // really should be something else (this will be wrong if ResLoad is false)
  116.  
  117.         // put it into the command buffer    
  118.         HLock(theUAMName);
  119.         memcpy(cmd,(const char *)&((*theUAMName)[0]),(*theUAMName)[0]+1);
  120.         cmd += (*theUAMName)[0]+1;
  121.         HUnlock(theUAMName);
  122.         ReleaseResource(theUAMName);
  123.         
  124.         // copy in the username
  125.         memcpy(cmd,(const char *)&user[0],user[0]+1);
  126.         cmd += user[0]+1;
  127.         if(((UInt32)cmd - (UInt32)commandBuffer) & 0x01)    // is this an odd boundry?
  128.         {
  129.             *cmd++ = 0x00;    // put in some padding
  130.         } 
  131.         
  132.         // copy in the password, only 8 bytes of password
  133.         memcpy(cmd,(const char *)password,8);
  134.         cmd += 8;
  135.         
  136.         // get the cmd buffer size
  137.         cmdSize = (unsigned long)((unsigned long)cmd - (unsigned long)commandBuffer);
  138.  
  139.         message.commandCode = kOpenSession;
  140.         message.cmdBuffer = commandBuffer;
  141.         message.cmdBufferSize = cmdSize;
  142.         message.replyBuffer = nil;
  143.         message.replyBufferSize = 0;
  144.         message.completion = nil;
  145.         message.contextPtr = nil;
  146.         
  147.         //DebugStr("\pabout to make the login call");
  148.          
  149.         theError = theArgs->callbacks->OpenSessionUPP(theArgs->Opt.auth.srvrAddress,nil,&message);
  150.         if(!theError){
  151.             theArgs->sessionRefNum = message.sessionRefNum;
  152.         }
  153.         theError = message.result;
  154.         
  155.     }
  156.     return theError;    
  157.  
  158. }
  159.  
  160.  
  161.  
  162. StringPtr    FigureAFPVersion(AFPSrvrInfo *info,ClientUAMCallbackRec *callbacks)
  163. {
  164.     struct    AFPClientInfo *theClientInfo = nil;
  165.     short    index;
  166.     Ptr        versBuf;
  167.     UInt32    versBufsize;
  168.     GetClientInfoPtr    *fcn;
  169.     
  170.     //DebugStr("\pFigureAFPVersion");
  171.     callbacks->GetClientInfoUPP(kAFPClientInfo,(ClientInfo **)&theClientInfo);
  172.  
  173.     if(theClientInfo){                
  174.     // go through the list of AFP versions supported and try to find them
  175.     // in the SrvrInfoBuffer, first match gets it 
  176.         versBuf = (Ptr)((UInt32)info + info->fVerCountOffset+1);
  177.         versBufsize = kMaxAFPCommand - info->fVerCountOffset;    // the largest size
  178.         
  179.         for(index = 0; index < theClientInfo->fNumAFPVersions; index++){
  180.             if(FindStringInBuf(theClientInfo->fAFPVersionStrs[index],versBuf,versBufsize)){
  181.                 return theClientInfo->fAFPVersionStrs[index];
  182.             }
  183.         }
  184.     }
  185.     return    nil;
  186. }
  187.  
  188. // rather clunky but it works
  189. Boolean        FindStringInBuf(StringPtr string, Ptr buf, UInt32 bufSize)
  190. {
  191.     Ptr        end = buf + bufSize;
  192.     Byte    len = string[0] + 1;
  193.     short    index;
  194.     
  195.     while((buf < end) && (*buf++ != string[0])) ;    // scan for the proper length
  196.  
  197.     if(!(buf < end)){
  198.         return false;
  199.     }
  200.     for(index = 1; (index < len) && (buf > end); index++){
  201.         if(*buf++ != string[index])
  202.             return false; 
  203.     }
  204.     if(!(buf < end)){
  205.         return false;
  206.     }
  207.     return true;
  208. }